﻿// master1.master.cs created with MonoDevelop
// User: armando at 22:03 30/01/2009
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Collections.Specialized;

namespace webpman
{
	
	
	public partial class master1 : System.Web.UI.MasterPage
	{
		
		System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
		public General MasterGeneral;






		
		protected void Page_Load(object sender, EventArgs e)
        {
			if ((Session["USER"] == null) && (this.Page.ToString().ToLower() != "asp.login_aspx" ))
			{
				Response.Redirect("login.aspx?callpage=" + Request.Url.PathAndQuery.Replace("&", "_-_") );
				return;
			}


			string TopHeader = MasterGeneral.GetTemplatePage(Server.MapPath("~/skins/" + MasterGeneral.PageSkin + "/top.html"));
			
			// Update buttons 
			string TopButton;
			
			// For all authenticated users
			if ((string)Session["AUTH"] == "A" || (string)Session["AUTH"] == "P" || (string)Session["AUTH"] == "U")
			{
				TopButton = MasterGeneral.RCMan.GetString("tb_cel_projects_header");
				TopButton = TopButton.Replace("<!-- CONTENT -->", MasterGeneral.RMan.GetString("tb_projects") );			
				TopHeader = TopHeader.Replace("<!-- tb_projects -->", TopButton);
				
				TopButton = MasterGeneral.RCMan.GetString("tb_cel_tasks_header");
				TopButton = TopButton.Replace("<!-- CONTENT -->", MasterGeneral.RMan.GetString("tb_tasks") );			
				TopHeader = TopHeader.Replace("<!-- tb_tasks -->", TopButton);
	
				TopButton = MasterGeneral.RCMan.GetString("tb_cel_items_header");
				TopButton = TopButton.Replace("<!-- CONTENT -->", MasterGeneral.RMan.GetString("tb_items") );			
				TopHeader = TopHeader.Replace("<!-- tb_items -->", TopButton);
			}
			
			// Only for administrator
			if ((string)Session["AUTH"] == "A")
			{
				TopButton = MasterGeneral.RCMan.GetString("tb_cel_categories_header");
				TopButton = TopButton.Replace("<!-- CONTENT -->", MasterGeneral.RMan.GetString("tb_categories") );			
				TopHeader = TopHeader.Replace("<!-- tb_categories -->", TopButton);
	
				TopButton = MasterGeneral.RCMan.GetString("tb_cel_states_header");
				TopButton = TopButton.Replace("<!-- CONTENT -->", MasterGeneral.RMan.GetString("tb_states") );			
				TopHeader = TopHeader.Replace("<!-- tb_states -->", TopButton);
	
				TopButton = MasterGeneral.RCMan.GetString("tb_cel_users_header");
				TopButton = TopButton.Replace("<!-- CONTENT -->", MasterGeneral.RMan.GetString("tb_users") );			
				TopHeader = TopHeader.Replace("<!-- tb_users -->", TopButton);
			}

			// For all authenticated users
			if ((string)Session["AUTH"] == "A" || (string)Session["AUTH"] == "P" || (string)Session["AUTH"] == "U")
			{
				TopButton = MasterGeneral.RCMan.GetString("tb_cel_logout_header");
				TopButton = TopButton.Replace("<!-- CONTENT -->", MasterGeneral.RMan.GetString("tb_logout") );			
				TopHeader = TopHeader.Replace("<!-- tb_login -->", TopButton);
			}
			
//			TopButton = MasterGeneral.RCMan.GetString("tb_cel_items_header");
//			TopButton = TopButton.Replace("<!-- CONTENT -->", MasterGeneral.RMan.GetString("tb_projects") );			
//			TopHeader = TopHeader.Replace("<!-- tb_projects -->", TopButton);


			// Update Languages			
			string head_languages = "<span class=\"TopLanguage\">" + MasterGeneral.RMan.GetString("head_language") + ":</span>";
			head_languages += "<select class=\"TopLanguage\" onchange=\"ChangeLang(this.value);\">";

			NameValueCollection MyLanguages = ((NameValueCollection)System.Web.Configuration.WebConfigurationManager.GetWebApplicationSection("sitelanguages"));
			
			for (int j=0; j<MyLanguages.Count; j++)
			{
				head_languages += "<option ";
				if (MyLanguages.GetValues(j)[0] == MasterGeneral.UserLanguage)
					head_languages += " selected ";
				
				head_languages += " value=\"" + MyLanguages.GetValues(j)[0] + "\">" + MyLanguages.Keys[j] + "</option>";
			}
			
			head_languages += "</select>";
			TopHeader = TopHeader.Replace("<!-- LANGUAGES -->", head_languages);

			// Update User Logged			
			if (Session["USER"] != null)
			{
				 TopHeader = TopHeader.Replace("<!-- USERACCOUNT -->", 
					"<span class=\"TopLanguage\">" + 
				    MasterGeneral.RMan.GetString("useraccount") + "&nbsp;" +
				    Session["USER"] + 
				    "&nbsp;&nbsp;&nbsp;[<a class=\"TopLanguage\" style=\"text-decoration:none;cursor:pointer\" href=\"changepwd.aspx\">" + MasterGeneral.RMan.GetString("changepassword") + "</a>]</span>");
			}
			
			// Update MasterPage Header
			PH_main.InnerHtml = TopHeader;
			
				
			// Update MasterPage Footer
			string FMP = MasterGeneral.GetTemplatePage(Server.MapPath("~/skins/" + MasterGeneral.PageSkin + "/bottom.html"));
			Version MyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
			
			FMP = FMP.Replace("<!-- pagerender-->", " - " + MasterGeneral.RMan.GetString("pagetime"));
			FMP = FMP.Replace("<!-- appversion  -->", MyVersion.ToString());

			PH_footer.InnerHtml = FMP; 
			
			
			
			// Update PathAndQuery ClientSide
			string PAQ = Request.Url.PathAndQuery.Replace("&","_-_");
			PH_main.InnerHtml+=  MasterGeneral.GetTemplatePage(Server.MapPath("~/javascripts/ChangeLang_function.js")).Replace("<!-- PathAndQuery -->", PAQ);
			
			
			
			MasterGeneral.DBClose();
		}


		protected override void Render(System.Web.UI.HtmlTextWriter writer) 
		{
        	// wait till the last possible moment to see what our stopwatch 
			// has for its elapsed time here we can get 
			// Elapsed Milliseconds, Elapsed Ticks, or an Elapsed TimeSpan
        	PH_footer.InnerHtml = PH_footer.InnerHtml.Replace("<!-- seconds -->", ((float)sw.Elapsed.Milliseconds / 1000).ToString("n2"));
        	base.Render(writer);
    	}


		
	}
}
